package com.example.lenovo.weiyi;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.com.ruanmeng.demon.SYM;
import com.com.ruanmeng.demon.VideoListM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.ImgDataUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.com.ruanmeng.view.CircularImage;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.BaseLoadMoreHeaderAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * 视频列表
 */
public class VideoListActivity extends BaseActivity {

    @BindView(R.id.rlist_video)
    RecyclerView rlistVideo;
    @BindView(R.id.refresh_video)
    SwipeRefreshLayout mRefresh;
    private LinearLayoutManager linearLayoutManager;
    ArrayList<VideoListM.RowsBean> Temp_list = new ArrayList<>();
    private VideoListAdapter mAdapter;
    private int page = 1;
    private LinearLayout li_video_null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);
        ButterKnife.bind(this);
        ChangLayTitle("视频");
        LayBack();
        init();
        getData();
    }

    public void init() {
        ///空
        li_video_null = (LinearLayout) findViewById(R.id.li_video_null);
        mRefresh.setColorSchemeResources(R.color.MainColor);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }
        });
        linearLayoutManager = new LinearLayoutManager(this);
        rlistVideo.setLayoutManager(linearLayoutManager);
        mAdapter = new VideoListAdapter(VideoListActivity.this, rlistVideo, Temp_list, R.layout.item_video);
        rlistVideo.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(VideoListActivity.this, VideoXQActivity.class);
                intent.putExtra("title",Temp_list.get(position+1).getTitle());
                intent.putExtra("intro",Temp_list.get(position+1).getIntro());
                intent.putExtra("link",Temp_list.get(position+1).getLink());
                startActivity(intent);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });
    }


    private void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.VideoList, Const.POST);
        mRequest.add("fileType", "1");//(1:视频,2音频)
        mRequest.add("page", page);
        CallServer.getRequestInstance().add(VideoListActivity.this, 0, mRequest,
                new CustomHttpListener(VideoListActivity.this, true, VideoListM.class) {

                    @Override
                    public void doWork(Object data, boolean isOne) {
                        VideoListM model = ((VideoListM) data);
                        if (page == 1) {
                            Temp_list.clear();
                        }
                        Temp_list.addAll(model.getRows());
                        if (Temp_list.size() == 0) {
                            li_video_null.setVisibility(View.VISIBLE);
                            rlistVideo.setVisibility(View.GONE);
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        mRefresh.setRefreshing(false);
                    }
                }, true, true);
    }

    public class VideoListAdapter extends BaseLoadMoreHeaderAdapter<VideoListM.RowsBean> {


        public VideoListAdapter(Context mContext, RecyclerView recyclerView, List<VideoListM.RowsBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, RecyclerView.ViewHolder holder, VideoListM.RowsBean s) {
            ImageView pic = (ImageView) holder.itemView.findViewById(R.id.imv_video_pic);
            TextView name = (TextView) holder.itemView.findViewById(R.id.tv_video_title);
            TextView content = (TextView) holder.itemView.findViewById(R.id.tv_video_content);
            View li_ban = (View) holder.itemView.findViewById(R.id.view_video_ban);
            View li_man = (View) holder.itemView.findViewById(R.id.view_video_man);
            ImgDataUtil.loadYuanImage(VideoListActivity.this,HttpIP.Base_IpIMage+s.getCover(),pic);
            name.setText(s.getTitle());
            content.setText(s.getIntro());
            if (holder.getLayoutPosition() == Temp_list.size() - 1) {
                li_ban.setVisibility(View.GONE);
                li_man.setVisibility(View.VISIBLE);
            } else {
                li_ban.setVisibility(View.VISIBLE);
                li_man.setVisibility(View.GONE);
            }
        }
    }
}
